<?php
declare (strict_types=1);

namespace app\common\listener;

use app\common\cache\queue\QueueEnum;
use app\common\token\Token;
use think\facade\Queue;

/**
 * 用户日志监听
 * Class UserLog
 * @package app\listener
 */
class UserLog
{
    /**
     * 事件监听处理
     * @return mixed
     */
    public function handle($response)
    {
        $member_info = Token::instance()->getUserInfo();
        if (!empty($member_info)) {
            $request = request();
            $data = [
                'creater_id' => $member_info[$member_info->getPk()],
                'updater_id' => $member_info[$member_info->getPk()],
                'host' => $request->host(),
                'module' => app('http')->getName(),
                'controller' => $request->controller(),
                'method' => $request->action(),
                'action_get' => json_encode($request->get(), JSON_UNESCAPED_UNICODE),
                'action_post' => json_encode($request->post(), JSON_UNESCAPED_UNICODE),
                'create_date' => date("Y-m-d H:i:s"),
                'response' => json_encode($response, JSON_UNESCAPED_UNICODE),
            ];
            $job = QueueEnum::ADMIN_WRITE_ACTIONLOG;
            $res = Queue::push($job, $data, QueueEnum::QUEUE_MAP[$job]);
            return $res;
        }
        return false;
    }
}
